﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_CompletChange : CommonPageBase
    {
        List<TempFile> files = new List<TempFile>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //txt_ATTEND.OnClientTriggerClick = Window1.GetSaveStateReference(hid_ATTEND.ClientID, txt_ATTEND.ClientID)
                //   + Window1.GetShowReference("/PublicModel/SelectUsers.aspx", "选择人员");
                //txt_CUTNUB.OnClientTriggerClick = Window1.GetSaveStateReference(hid_CUTNUB.ClientID, txt_CUTNUB.ClientID)
                //   + Window1.GetShowReference("/PublicModel/SelectCustom.aspx", "选择客户");

                txt_PRJNUB.OnClientTriggerClick = Window1.GetSaveStateReference(txt_Proj_Name.ClientID, txt_PRJNUB.ClientID)
                   + Window1.GetShowReference("/PublicModel/SelectProj.aspx?mtype=1", "选择项目");


                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                
               // DataBaseHelper.BindDrp(Drp_PERTAX, EnumData.DataBaseEunm.TaxRate);

                // DataBaseHelper.BindDrp(Drp_PAYMET, EnumData.DataBaseEunm.PayMet);

                LoadData();
                BindGrid();
            }
        }

        protected void LoadData()
        {
            //if (!string.IsNullOrEmpty(Request["cid"]))
            //{
            //    Proj_Main pm = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["cid"])).ToFirst();
            //    txt_Proj_No.Text = pm.PRJNUB;
            //    txt_Proj_Name.Text = pm.DESTON;
            //}
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Complet Con = TbsDbSession.Default.From<Proj_Complet>().Where(Proj_Complet._.ID == int.Parse(Request["id"])).ToFirst();
                if (Con != null)
                {
                   txt_PRJNUB.Text = Con.Proj_No;
                    txt_Proj_Name.Text = Con.Proj_Name;
                    txt_ReMark.Text = Con.ReMark;
                    hf.Text = Con.AttachFiles;
                    if (Con.EDTIME.HasValue)
                        txt_EDTIME.Text = Con.EDTIME.Value.ToString("yyyy-MM-dd");
                }
            }
            if (!string.IsNullOrEmpty(Request["pid"]))
            {

                Proj_Main pm = TbsDbSession.Default.From<Proj_Main>().Where(Proj_Main._.ID == int.Parse(Request["pid"])).ToFirst();
                if (pm != null)
                {
                    txt_PRJNUB.Text = pm.PRJNUB;
                    txt_Proj_Name.Text = pm.DESTON;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Proj_Complet Con = new Proj_Complet();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Con.ID = int.Parse(Request["id"]);
                Con.Attach();
                Con.ReMark = txt_ReMark.Text;
                Con.AttachFiles = hf.Text;
                Con.EDTIME = Convert.ToDateTime(txt_EDTIME.Text);
                //Con.CONNAME = txt_CONNAME.Text;
                returnvalue = TbsDbSession.Default.Update<Proj_Complet>(Con);
            }
            else
            {
                Con.DeAttach();
                Con.Proj_Name = txt_Proj_Name.Text;
                Con.Proj_No = txt_PRJNUB.Text;
                Con.ReMark = txt_ReMark.Text;
                Con.AttachFiles = hf.Text;
                Con.AddTime = DateTime.Now;
                Con.Adder = CookInfo.EAdminTrueName();
                Con.AddID = CookInfo.EAdminid();
                Con.EDTIME =Convert.ToDateTime(txt_EDTIME.Text);
                returnvalue = TbsDbSession.Default.Insert<Proj_Complet>(Con);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }

    }
}